package com.outsource.framework.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

public class QueryParamer<T> implements Specification<T> {

	private Map<String, String> map = null;
	
	public QueryParamer(Map<String, String> map) {
		// TODO Auto-generated constructor stub
		this.map = map;
	}

	@Override
	public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
			CriteriaBuilder cb) {
		// TODO Auto-generated method stub
		List<Predicate> list = new ArrayList<Predicate>();
		Set<String> set = map.keySet();
		for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			Predicate param = cb.equal(root.get(key),map.get(key));
			list.add(param);
		}
		Predicate[] p = new Predicate[list.size()];
		list.toArray(p);
		query.where(p);
		return null;
	}

}
